<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">名称</label>
        <el-input
          v-model="query.name"
          clearable
          placeholder="名称"
          style="width: 185px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <label class="el-form-item-label">通风柜ip</label>
        <el-input
          v-model="query.ip"
          clearable
          placeholder="通风柜ip"
          style="width: 185px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <label class="el-form-item-label">实验室</label>
        <el-select
          v-model="query.labId"
          clearable
          style="width: 185px"
          placeholder="实验室"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        >
          <el-option
            v-for="item in labs"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
        <!-- <label class="el-form-item-label">排风量</label>
        <el-input
          v-model="query.exhaustAirRate"
          clearable
          placeholder="排风量"
          style="width: 185px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        /> -->
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="550px"
      >
        <el-form
          ref="form"
          :model="form"
          :rules="rules"
          size="small"
          label-width="115px"
        >
          <el-form-item label="名称" prop="name">
            <el-input v-model="form.name" style="width: 370px" />
          </el-form-item>
          <el-form-item label="通风柜ip" prop="ip">
            <el-input v-model="form.ip" style="width: 370px" />
          </el-form-item>
          <el-form-item label="启动地址" prop="openAddress">
            <el-input v-model="form.openAddress" style="width: 370px" />
          </el-form-item>
          <el-form-item label="关闭地址" prop="closeAddress">
            <el-input v-model="form.closeAddress" style="width: 370px" />
          </el-form-item>
          <el-form-item label="启停状态地址" prop="address">
            <el-input v-model="form.address" style="width: 370px" />
          </el-form-item>
          <el-form-item label="实验室" prop="labId">
            <el-select
              v-model="form.labId"
              style="width: 370px"
              clearable
              filterable
              :remote-method="getLabs"
              placeholder="请选择实验室"
            >
              <el-option
                v-for="item in labs"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <!-- <el-form-item label="排风量" prop="exhaustAirRate">
            <el-input v-model="form.exhaustAirRate" style="width: 370px" />
          </el-form-item> -->
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button
            :loading="crud.status.cu === 2"
            type="primary"
            @click="crud.submitCU"
            >确认</el-button
          >
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="ip" label="通风柜ip" />
        <!-- <el-table-column prop="address" label="地址" /> -->
        <el-table-column prop="labName" label="实验室" />
        <!-- <el-table-column prop="exhaustAirRate" label="排风量">
          <template slot-scope="scope">
            <div v-if="scope.row.exhaustAirRate == null">- -</div>
            <div v-if="scope.row.exhaustAirRate">{{ scope.row.exhaustAirRate }}m/s</div>
            < {{ scope.row.exhaustAirRate }}m/s >
          </template>
        </el-table-column>  -->
        <el-table-column prop="openAddress" label="启动地址" />
        <el-table-column prop="closeAddress" label="关闭地址" />
        <el-table-column prop="address" label="启停状态地址" />
        <el-table-column prop="isOpen" label="启停状态">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.isOpen == 1"
              >开启</el-tag
            >
            <el-tag type="danger" v-if="scope.row.isOpen == 0"
              >停止</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column
          v-if="checkPer(['admin', 'fumeHood:edit', 'fumeHood:del'])"
          label="操作"
          width="250px"
          align="center"
        >
          <template slot-scope="scope">
            <udOperation :data="scope.row" :permission="permission" />
            <el-button
              size="mini"
              type="success"
              @click="toOpenDoor(scope.row.id)"
              >开启</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="toCloseDoor(scope.row.id)"
              >停止</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudFumeHood from "@/api/fumeHood";
import CRUD, { presenter, header, form, crud } from "@crud/crud";
import rrOperation from "@crud/RR.operation";
import crudOperation from "@crud/CRUD.operation";
import udOperation from "@crud/UD.operation";
import pagination from "@crud/Pagination";
import { get } from "@/api/lab";
import { openDoor, closeDoor } from "@/api/fumeHood";

const defaultForm = {
  id: null,
  name: null,
  ip: null,
  labId: null,
  exhaustAirRate: null,
  createdAt: null,
  updatedAt: null,
  isDeleted: null,
  address: null,
};
export default {
  name: "FumeHood",
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: "通风柜",
      url: "api/fumeHood",
      idField: "id",
      sort: "id,desc",
      crudMethod: { ...crudFumeHood },
    });
  },
  data() {
    return {
      permission: {
        add: ["admin", "fumeHood:add"],
        edit: ["admin", "fumeHood:edit"],
        del: ["admin", "fumeHood:del"],
      },
      rules: {
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        ip: [{ required: true, message: "通风柜ip不能为空", trigger: "blur" }],
        labId: [{ required: true, message: "实验室不能为空", trigger: "blur" }],
        openAddress: [
          { required: true, message: "启动地址不能为空", trigger: "blur" },
        ],
        closeAddress: [
          { required: true, message: "关闭地址不能为空", trigger: "blur" },
        ],
        address: [
          { required: true, message: "启停状态地址不能为空", trigger: "blur" },
        ],
        exhaustAirRate: [
          { required: true, message: "排风量不能为空", trigger: "blur" },
        ],
      },
      queryTypeOptions: [
        { key: "name", display_name: "名称" },
        { key: "ip", display_name: "通风柜ip" },
        { key: "labId", display_name: "实验室" },
        { key: "exhaustAirRate", display_name: "排风量" },
      ],
      labs: [],
    };
  },
  created() {
    this.getLabs();
  },
  methods: {
    getLabs() {
      get()
        .then((response) => {
          this.labs = response;
        })
        .catch((_) => {
          console.log(response);
        });
    },
    toOpenDoor(id) {
      const loading = this.$loading({
        lock: true,
        text: "远程连接失败，请刷新页面重试...",
        spinner: "el-icon-loading",
        background: "rgba(255,255,255,0.8)",
      });
      openDoor({
        id,
      }).then((response) => {
        loading.close();
        this.$message.success("已开启");
        this.crud.refresh();
      });
    },
    toCloseDoor(id) {
      const loading = this.$loading({
        lock: true,
        text: "远程连接失败，请刷新页面重试...",
        spinner: "el-icon-loading",
        background: "rgba(255,255,255,0.8)",
      });
      closeDoor({
        id,
      }).then((response) => {
        loading.close();
        this.$message.warning("已关闭");
        this.crud.refresh();
      });
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true;
    },
  },
};
</script>

<style scoped lang="scss">
.el-table ::v-deep td.el-table__cell div {
  display: flex;
  align-items: center;

  .el-button {
    width: 43px;
    height: 28px;
    line-height: 28px;
    text-align: center;
    margin-left: 5px !important;
    padding: 0;
  }
}
</style>
